package com.example.hehecat.yuejian1.ui.fragment;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hehecat.yuejian1.R;
import com.example.hehecat.yuejian1.mvp.contract.PictureInfoContract;
import com.example.hehecat.yuejian1.mvp.model.PictureInfoModel;
import com.example.hehecat.yuejian1.mvp.model.bean.PictureBean;
import com.example.hehecat.yuejian1.mvp.presenter.PictureInfoPresenter;
import com.example.hehecat.yuejian1.ui.wiget.MyStaticVariable;
import com.squareup.picasso.Picasso;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;
import rx.Subscriber;

import static com.chad.library.adapter.base.listener.SimpleClickListener.TAG;


public class PictureInfoFragment extends SupportFragment implements PictureInfoContract.View {


    @BindView(R.id.picture_img)
    ImageView pictureImg;
    @BindView(R.id.title)
    AppCompatTextView title;
    @BindView(R.id.content)
    AppCompatTextView content;
    @BindView(R.id.picture_time)
    TextView pictureTime;
    /*@BindView(R.id.comment)
    Button comment;*/
    @BindView(R.id.comment_recyclerview)
    RecyclerView commentRecyclerview;
    Unbinder unbinder;

    int picture_id;
    int user_id;


    PictureInfoPresenter presenter;
    @BindView(R.id.pictureinfo_return)
    ImageView pictureinfoReturn;
    @BindView(R.id.TextView)
    TextView TextView;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_picture_info, container, false);
        unbinder = ButterKnife.bind(this, view);

        if (getArguments() != null) {
            picture_id = getArguments().getInt("picture_id");
        }
        user_id = MyStaticVariable.newInstance().getUserid();
        Log.d(TAG, "onCreateView:picture_id " + picture_id);


        PictureInfoModel model = new PictureInfoModel();
        model.getPicture(picture_id, new Subscriber<PictureBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(PictureBean pictureBean) {
                Log.d(TAG, "onNext: " + pictureBean.getPicturename());
                pictureTime.setText(pictureBean.getData());
                content.setText(pictureBean.getIntro());
//                Picasso.with(getContext()).load("http://10.0.2.2/yj1/public/uploads/" + pictureBean.getPicture1()).into(pictureImg);
                Picasso.with(getContext()).load("http://39.96.5.124/yj1/public/uploads/" + pictureBean.getPicture1()).into(pictureImg);
            }
        });
        return view;
    }

    @Override
    public void showMsg(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }



    @OnClick(R.id.pictureinfo_return)
    void pictureInfoReturn(){
        PictureFragment fragment = new PictureFragment();
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content,fragment,null)
                .commit();
    }

    public static PictureInfoFragment newInstance(int picture_id) {
        // Required empty public constructor

        PictureInfoFragment fragment = new PictureInfoFragment();
        Bundle args = new Bundle();
        args.putInt("picture_id", picture_id);
        fragment.setArguments(args);
        return fragment;
    }
}
